package business_opportunity

import (
	"context"
	"errors"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/business_opportunity"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/business_opportunity/contact"
	"git.myscrm.cn/golang/stark/v4"
)

type contactServer struct {
	contactService contact.ContactServiceIface
}

func NewContact(contactService contact.ContactServiceIface,
) pb.ContactServiceServer {
	return &contactServer{
		contactService: contactService,
	}
}

// ContactList 联系列表
func (s *contactServer) ContactList(ctx context.Context, request *pb.ContactListRequest) (*pb.ContactListResponse, error) {
	res, err := s.contactService.ContactList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "ContactList 获取联系人列表失败，参数:%+v，错误信息：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// ContactDetail 联系人详情
func (s *contactServer) ContactDetail(ctx context.Context, request *pb.ContactDetailRequest) (*pb.ContactDetailResponse, error) {
	var errMsg string
	if request.GetId() == "" {
		errMsg = "id参数缺失"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}
	res, err := s.contactService.ContactDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "ContactDetail 获取联系人详情失败，参数:%+v, 错误信息：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// CooperationList 联系人合作案例
func (s *contactServer) CooperationList(ctx context.Context, request *pb.CooperationListRequest) (*pb.CooperationListResponse, error) {
	var errMsg string
	if request.GetPhoneNumber() == "" {
		errMsg = "phone_number参数缺失"
	}
	if request.GetCategory() == "" {
		errMsg = "category参数缺失"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}
	res, err := s.contactService.CooperationList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "CooperationList 获取合作案例失败，参数:%+v, 错误信息：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// GetMobile 获取手机号
func (s *contactServer) GetMobile(ctx context.Context, request *pb.GetMobileRequest) (*pb.GetMobileResponse, error) {
	var errMsg string
	if request.GetId() == "" {
		errMsg = "id参数缺失"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}
	res, err := s.contactService.GetMobile(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "GetMobile 获取手机号失败，参数:%+v, 错误信息：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}

// CompanyContactCnt 获取公司联系人数量
func (s *contactServer) CompanyContactCnt(ctx context.Context, request *pb.CompanyContactCntRequest) (*pb.CompanyContactCntResponse, error) {
	var errMsg string
	if request.GetCompanyName() == "" {
		errMsg = "id参数缺失"
	}
	if errMsg != "" {
		return nil, errors.New(errMsg)
	}
	res, err := s.contactService.CompanyContactCnt(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "CompanyContactCnt 获取公司联系人数量失败，参数:%+v, 错误信息：%s", request, err.Error())
		return nil, err
	}
	return res, nil
}
